package shuiyuqing.model;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import managers.DatabaseManager;
import shuiyuqing.network.beans.IrrBAWaterGate;
import shuiyuqing.network.beans.IrrBRStadia;
import shuiyuqing.network.beans.IrrBTCanalDetail;

/**
 * 获取信息没错，但有欠缺可能,
 * Created by 凯 on 2015/11/5.
 */
public class BasicInfoModel {

    // 渠道基本信息
    List<IrrBTCanalDetail> irrBTCanalDetails = new ArrayList<IrrBTCanalDetail>();

    // 测站信息
    List<IrrBRStadia> irrBRStadias = new ArrayList<IrrBRStadia>();

    // 水闸信息
    List<IrrBAWaterGate> irrBAWaterGates = new ArrayList<IrrBAWaterGate>();

    // 水闸有测站
    List<WaterGateWithStadia> waterGateWithStadias = new ArrayList<WaterGateWithStadia>();

    // 渠道有测站
    List<CannelWithStadia> cannelWithStadias = new ArrayList<CannelWithStadia>();


    private static BasicInfoModel model;

    @Deprecated
    public static BasicInfoModel share() {
        System.out.println();
        if (model == null) {
            model = initFromDB();
        }
        return model;
    }

    // 从数据库初始化
    private static BasicInfoModel initFromDB() {

        BasicInfoModel basicInfoModel = new BasicInfoModel();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {

            // 获取渠道信息
            Cursor c1 = db.rawQuery("select * from t_IrrBTCanalDetail", new String[]{});
            while (c1.moveToNext()) {
                IrrBTCanalDetail detail = new IrrBTCanalDetail();
                detail.setCanalCode(c1.getString(c1.getColumnIndex("canalCode")));
                detail.setCanalName(c1.getString(c1.getColumnIndex("canalName")));

                String canalG = c1.getString(c1.getColumnIndex("canalG"));
                if (canalG != null && !canalG.equals("null")) {
                    detail.setCanalG(canalG.charAt(0));
                }
//
                String canalDesQ = c1.getString(c1.getColumnIndex("canalDesQ"));
                if (canalDesQ != null && !canalDesQ.equals("null")) {
                    detail.setCanalDesQ(new BigDecimal(canalDesQ));
                }

                String leakR = c1.getString(c1.getColumnIndex("leakR"));
                if (leakR != null && !leakR.equals("null")) {
                    detail.setLeakR(new BigDecimal(leakR));
                }

                String canalLen = c1.getString(c1.getColumnIndex("canalLen"));
                if (canalLen != null && !canalLen.equals("null")) {
                    detail.setCanalLen(new BigDecimal(canalLen));
                }

                String lineLen = c1.getString(c1.getColumnIndex("lineLen"));
                if (lineLen != null && !lineLen.equals("null")) {
                    detail.setLineLen(new BigDecimal(lineLen));
                }

                detail.setEngManCd(c1.getString(c1.getColumnIndex("engManCd")));
                detail.setUperCanalCd(c1.getString(c1.getColumnIndex("uperCanalCd")));

                String timeSign = c1.getString(c1.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    detail.setTimeSign(calendar);
                }

                detail.setRemark(c1.getString(c1.getColumnIndex("remark")));

                detail.setStadiaCode(c1.getString(c1.getColumnIndex("stadiaCode")));
                basicInfoModel.irrBTCanalDetails.add(detail);
            }

            c1.close();

            // 获取闸门信息
            Cursor c2 = db.rawQuery("select * from t_IrrBAWaterGate", null);
            while (c2.moveToNext()) {
                IrrBAWaterGate gate = new IrrBAWaterGate();
                gate.setWatergateCode(c2.getString(c2.getColumnIndex("watergateCode")));
                gate.setCanalCode(c2.getString(c2.getColumnIndex("canalCode")));
                gate.setWatergateName(c2.getString(c2.getColumnIndex("watergateName")));
                gate.setWatergateType(c2.getString(c2.getColumnIndex("watergateType")));
                gate.setWatergateMET(c2.getString(c2.getColumnIndex("watergateMET")));
                gate.setWatergateLC(c2.getString(c2.getColumnIndex("watergateLC")));

                String watergateNUM = c2.getString(c2.getColumnIndex("watergateNUM"));
                if (watergateNUM != null && !watergateNUM.equals("null")) {
                    gate.setWatergateNUM(new BigDecimal(watergateNUM));
                }

                String designWaterLevel = c2.getString(c2.getColumnIndex("designWaterLevel"));
                if (designWaterLevel != null && !designWaterLevel.equals("null"))
                {
                    gate.setWatergateQ(new BigDecimal(designWaterLevel));
                }

                String watergateHeight = c2.getString(c2.getColumnIndex("watergateHeight"));
                if (watergateHeight != null && !watergateHeight.equals("null")) {
                    gate.setWatergateHeight(new BigDecimal(watergateHeight));
                }

                String watergateWtdth = c2.getString(c2.getColumnIndex("watergateWtdth"));
                if (watergateWtdth != null && !watergateWtdth.equals("null")) {
                    gate.setWatergateWtdth(new BigDecimal(watergateWtdth));
                }

                String watergateQ = c2.getString(c2.getColumnIndex("watergateQ"));
                if (watergateQ != null && !watergateQ.equals("null")) {
                    gate.setWatergateQ(new BigDecimal(watergateQ));
                }

                String watergateMTP = c2.getString(c2.getColumnIndex("watergateMTP"));
                if (watergateMTP != null && !watergateMTP.equals("null")) {
                    gate.setWatergateMTP(watergateMTP);
                }

                String watergateStatus = c2.getString(c2.getColumnIndex("watergateStatus"));
                if (watergateStatus != null && !watergateStatus.equals("null") && watergateStatus.length() > 0) {
                    gate.setWatergateStatus(watergateStatus.charAt(0));
                }

                gate.setEngManCd(c2.getString(c2.getColumnIndex("engManCd")));
                gate.setTelephone(c2.getString(c2.getColumnIndex("telephone")));

                String timeSign = c2.getString(c2.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    gate.setTimeSign(calendar);
                    gate.setRemark(c2.getString(c2.getColumnIndex("remark")));
                }

                gate.setStadiaCode(c2.getString(c2.getColumnIndex("stadiaCode")));

                basicInfoModel.irrBAWaterGates.add(gate);
            }

            c2.close();

            // 测站信息
            Cursor c3 = db.rawQuery("select * from t_IrrBRStadia", null);
            while (c3.moveToNext()) {
                IrrBRStadia irrBRStadia = new IrrBRStadia();
                irrBRStadia.setStadiaCode(c3.getString(c3.getColumnIndex("stadiaCode")));
                irrBRStadia.setStadiaName(c3.getString(c3.getColumnIndex("stadiaName")));
                irrBRStadia.setStadiaType(c3.getString(c3.getColumnIndex("stadiaType")));
                irrBRStadia.setLocCode(c3.getString(c3.getColumnIndex("locCode")));
                String stbk = c3.getString(c3.getColumnIndex("stbk"));
                if (stbk != null && !stbk.equals("null")) {
                    irrBRStadia.setStbk(stbk);
                }

                String flowDir = c3.getString(c3.getColumnIndex("flowDir"));
                if (flowDir != null && !flowDir.equals("null")) {
                    irrBRStadia.setFlowDir(flowDir);
                }

                irrBRStadia.setDatTp(c3.getString(c3.getColumnIndex("datTp")));
                String datElev = c3.getString(c3.getColumnIndex("datElev"));
                if (datElev != null && !datElev.equals("null")) {
                    irrBRStadia.setDatElev(new BigDecimal(datElev));
                }

                String modBasVal = c3.getString(c3.getColumnIndex("modBasVal"));
                if (modBasVal != null && !modBasVal.equals("null")) {
                    irrBRStadia.setModBasVal(new BigDecimal(modBasVal));
                }

                String modPara = c3.getString(c3.getColumnIndex("modPara"));
                if (modPara != null && !modPara.equals("null")) {
                    irrBRStadia.setModPara(new BigDecimal(modPara));
                }

                // 高程
                String elevation = c3.getString(c3.getColumnIndex("elevation"));
                if (elevation != null && !elevation.equals("null")) {
                    irrBRStadia.setElevation(new BigDecimal(elevation));
                }


                String estStYm = c3.getString(c3.getColumnIndex("estStYm"));
                if (estStYm != null && !estStYm.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(estStYm)));
                    irrBRStadia.setEstStYm(calendar);
                }



                String runCond = c3.getString(c3.getColumnIndex("runCond"));
                if (runCond != null && runCond.equals("null") && runCond.length() > 0) {
                    irrBRStadia.setRunCond(runCond);
                }

                irrBRStadia.setEngManCd(c3.getString(c3.getColumnIndex("engManCd")));

                String timeSign = c3.getString(c3.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    irrBRStadia.setTimeSign(calendar);
                }

                irrBRStadia.setRemark(c3.getString(c3.getColumnIndex("remark")));
                basicInfoModel.irrBRStadias.add(irrBRStadia);
            }

            c3.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        //建立水闸、渠道、测站关系
//        initrelation(basicInfoModel);

        return basicInfoModel;
    }


    // 清空基本信息表
    public static void clearBasicInfo()
    {
        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {

            db.execSQL("delete from t_IrrBAWaterGate");
            db.execSQL("delete from t_IrrBRStadia");
            db.execSQL("delete from t_IrrBTCanalDetail");
            db.setTransactionSuccessful();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }
    }

    // 通过水闸渠道获取测站信息
    @Deprecated
    private static void initrelation(BasicInfoModel basicInfoModel) {

        // 建立闸坝与测站关系
        for (IrrBAWaterGate gate : basicInfoModel.irrBAWaterGates) {
            WaterGateWithStadia temp = new WaterGateWithStadia();

            temp.setGate(gate);
            for (IrrBRStadia stadia : basicInfoModel.irrBRStadias) {
                if (stadia.getLocCode() == null || stadia.getLocCode().equals("null"))
                    continue;

                if (stadia.getLocCode().equals(gate.getWatergateCode())) {
                    temp.setStadia(stadia);
                    break;
                }
            }

            basicInfoModel.waterGateWithStadias.add(temp);
        }

        // 建立渠道与测站关系
        for (IrrBTCanalDetail detail : basicInfoModel.irrBTCanalDetails) {
            CannelWithStadia temp = new CannelWithStadia();
            temp.setDetail(detail);

            for (IrrBRStadia stadia : basicInfoModel.irrBRStadias) {
                if (stadia.getLocCode() == null || stadia.getLocCode().equals("null"))
                    continue;

                if (stadia.getLocCode().equals(detail.getCanalCode())) {
                    temp.setStadia(stadia);
                    break;
                }
            }
            basicInfoModel.cannelWithStadias.add(temp);

        }
    }

    // 清除内存
    @Deprecated
    public static void clear() {
        model = null;
    }

    // 保存渠道和测站联系
    @Deprecated
    public static class CannelWithStadia {
        IrrBTCanalDetail detail;
        IrrBRStadia stadia;

        public IrrBTCanalDetail getDetail() {
            return detail;
        }

        public void setDetail(IrrBTCanalDetail detail) {
            this.detail = detail;
        }

        public IrrBRStadia getStadia() {
            return stadia;
        }

        public void setStadia(IrrBRStadia stadia) {
            this.stadia = stadia;
        }
    }

    // 保存闸坝和测站关系
    @Deprecated
    public static class WaterGateWithStadia {
        IrrBAWaterGate gate;
        IrrBRStadia stadia;

        public IrrBAWaterGate getGate() {
            return gate;
        }

        public void setGate(IrrBAWaterGate gate) {
            this.gate = gate;
        }

        public IrrBRStadia getStadia() {
            return stadia;
        }

        public void setStadia(IrrBRStadia stadia) {
            this.stadia = stadia;
        }
    }


    // 获取所有的闸门的信息
    public static List<IrrBAWaterGate> getAllGate() {


        ArrayList<IrrBAWaterGate> gates = new ArrayList<IrrBAWaterGate>();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {

            // 获取闸门信息
            Cursor c2 = db.rawQuery("select * from t_IrrBAWaterGate", null);
            while (c2.moveToNext()) {
                IrrBAWaterGate gate = new IrrBAWaterGate();
                gate.setWatergateCode(c2.getString(c2.getColumnIndex("watergateCode")));
                gate.setCanalCode(c2.getString(c2.getColumnIndex("canalCode")));
                gate.setWatergateName(c2.getString(c2.getColumnIndex("watergateName")));
                gate.setWatergateType(c2.getString(c2.getColumnIndex("watergateType")));
                gate.setWatergateMET(c2.getString(c2.getColumnIndex("watergateMET")));
                gate.setWatergateLC(c2.getString(c2.getColumnIndex("watergateLC")));

                String watergateNUM = c2.getString(c2.getColumnIndex("watergateNUM"));
                if (watergateNUM != null && !watergateNUM.equals("null")) {
                    gate.setWatergateNUM(new BigDecimal(watergateNUM));
                }

                String watergateHeight = c2.getString(c2.getColumnIndex("watergateHeight"));
                if (watergateHeight != null && !watergateHeight.equals("null")) {
                    gate.setWatergateHeight(new BigDecimal(watergateHeight));
                }

                String watergateWtdth = c2.getString(c2.getColumnIndex("watergateWtdth"));
                if (watergateWtdth != null && !watergateWtdth.equals("null")) {
                    gate.setWatergateWtdth(new BigDecimal(watergateWtdth));
                }

                String watergateQ = c2.getString(c2.getColumnIndex("watergateQ"));
                if (watergateQ != null && !watergateQ.equals("null")) {
                    gate.setWatergateQ(new BigDecimal(watergateQ));
                }

                String watergateMTP = c2.getString(c2.getColumnIndex("watergateMTP"));
                if (watergateMTP != null && !watergateMTP.equals("null")) {
                    gate.setWatergateMTP(watergateMTP);
                }

                String watergateStatus = c2.getString(c2.getColumnIndex("watergateStatus"));
                if (watergateStatus != null && !watergateStatus.equals("null") && watergateStatus.length() > 0) {
                    gate.setWatergateStatus(watergateStatus.charAt(0));
                }

                gate.setEngManCd(c2.getString(c2.getColumnIndex("engManCd")));
                gate.setTelephone(c2.getString(c2.getColumnIndex("telephone")));

                String timeSign = c2.getString(c2.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    gate.setTimeSign(calendar);
                    gate.setRemark(c2.getString(c2.getColumnIndex("remark")));
                }

                gate.setStadiaCode(c2.getString(c2.getColumnIndex("stadiaCode")));
                gates.add(gate);

            }

            c2.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }
        return gates;
    }

    // 通过渠道获取到闸门
    public static List<IrrBAWaterGate> getGatesByCannal(String cannalCode) {
        ArrayList<IrrBAWaterGate> gates = new ArrayList<IrrBAWaterGate>();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {

            // 获取闸门信息
            Cursor c2 = db.rawQuery("select * from t_IrrBAWaterGate where canalCode=?", new String[]{cannalCode});
            while (c2.moveToNext()) {
                IrrBAWaterGate gate = new IrrBAWaterGate();
                gate.setWatergateCode(c2.getString(c2.getColumnIndex("watergateCode")));
                gate.setCanalCode(c2.getString(c2.getColumnIndex("canalCode")));
                gate.setWatergateName(c2.getString(c2.getColumnIndex("watergateName")));
                gate.setWatergateType(c2.getString(c2.getColumnIndex("watergateType")));
                gate.setWatergateMET(c2.getString(c2.getColumnIndex("watergateMET")));
                gate.setWatergateLC(c2.getString(c2.getColumnIndex("watergateLC")));

                String watergateNUM = c2.getString(c2.getColumnIndex("watergateNUM"));
                if (watergateNUM != null && !watergateNUM.equals("null")) {
                    gate.setWatergateNUM(new BigDecimal(watergateNUM));
                }

                String watergateHeight = c2.getString(c2.getColumnIndex("watergateHeight"));
                if (watergateHeight != null && !watergateHeight.equals("null")) {
                    gate.setWatergateHeight(new BigDecimal(watergateHeight));
                }

                String watergateWtdth = c2.getString(c2.getColumnIndex("watergateWtdth"));
                if (watergateWtdth != null && !watergateWtdth.equals("null")) {
                    gate.setWatergateWtdth(new BigDecimal(watergateWtdth));
                }

                String watergateQ = c2.getString(c2.getColumnIndex("watergateQ"));
                if (watergateQ != null && !watergateQ.equals("null")) {
                    gate.setWatergateQ(new BigDecimal(watergateQ));
                }

                String watergateMTP = c2.getString(c2.getColumnIndex("watergateMTP"));
                if (watergateMTP != null && !watergateMTP.equals("null")) {
                    gate.setWatergateMTP(watergateMTP);
                }

                String watergateStatus = c2.getString(c2.getColumnIndex("watergateStatus"));
                if (watergateStatus != null && !watergateStatus.equals("null") && watergateStatus.length() > 0) {
                    gate.setWatergateStatus(watergateStatus.charAt(0));
                }

                gate.setEngManCd(c2.getString(c2.getColumnIndex("engManCd")));
                gate.setTelephone(c2.getString(c2.getColumnIndex("telephone")));

                String timeSign = c2.getString(c2.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    gate.setTimeSign(calendar);
                    gate.setRemark(c2.getString(c2.getColumnIndex("remark")));
                }

                gate.setStadiaCode(c2.getString(c2.getColumnIndex("stadiaCode")));
                gates.add(gate);

            }

            c2.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }
        return gates;
    }


    // 获取所有的渠道信息
    public static List<IrrBTCanalDetail> getAllCannal() {

        ArrayList<IrrBTCanalDetail> details = new ArrayList<IrrBTCanalDetail>();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {

            // 获取渠道信息
            Cursor c1 = db.rawQuery("select * from t_IrrBTCanalDetail", new String[]{});
            while (c1.moveToNext()) {
                IrrBTCanalDetail detail = new IrrBTCanalDetail();
                detail.setCanalCode(c1.getString(c1.getColumnIndex("canalCode")));
                detail.setCanalName(c1.getString(c1.getColumnIndex("canalName")));

                String canalG = c1.getString(c1.getColumnIndex("canalG"));
                if (canalG != null && !canalG.equals("null")) {
                    detail.setCanalG(canalG.charAt(0));
                }
//
                String canalDesQ = c1.getString(c1.getColumnIndex("canalDesQ"));
                if (canalDesQ != null && !canalDesQ.equals("null")) {
                    detail.setCanalDesQ(new BigDecimal(canalDesQ));
                }

                String leakR = c1.getString(c1.getColumnIndex("leakR"));
                if (leakR != null && !leakR.equals("null")) {
                    detail.setLeakR(new BigDecimal(leakR));
                }

                String canalLen = c1.getString(c1.getColumnIndex("canalLen"));
                if (canalLen != null && !canalLen.equals("null")) {
                    detail.setCanalLen(new BigDecimal(canalLen));
                }

                String lineLen = c1.getString(c1.getColumnIndex("lineLen"));
                if (lineLen != null && !lineLen.equals("null")) {
                    detail.setLineLen(new BigDecimal(lineLen));
                }

                detail.setEngManCd(c1.getString(c1.getColumnIndex("engManCd")));
                detail.setUperCanalCd(c1.getString(c1.getColumnIndex("uperCanalCd")));

                String timeSign = c1.getString(c1.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    detail.setTimeSign(calendar);
                }

                detail.setRemark(c1.getString(c1.getColumnIndex("remark")));

                detail.setStadiaCode(c1.getString(c1.getColumnIndex("stadiaCode")));

                details.add(detail);
            }

            c1.close();
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        return details;
    }


    // 根据闸门获取测站信息
    public static IrrBRStadia getStadiaByGate(String gateCode) {

        IrrBRStadia stadia = null;

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {

            // 测站信息
            Cursor c3 = db.rawQuery("select * from t_IrrBRStadia where locCode=?", new String[]{gateCode});
            while (c3.moveToNext()) {
                IrrBRStadia irrBRStadia = new IrrBRStadia();
                irrBRStadia.setStadiaCode(c3.getString(c3.getColumnIndex("stadiaCode")));
                irrBRStadia.setStadiaName(c3.getString(c3.getColumnIndex("stadiaName")));
                irrBRStadia.setStadiaType(c3.getString(c3.getColumnIndex("stadiaType")));
                irrBRStadia.setLocCode(c3.getString(c3.getColumnIndex("locCode")));
                String stbk = c3.getString(c3.getColumnIndex("stbk"));
                if (stbk != null && !stbk.equals("null")) {
                    irrBRStadia.setStbk(stbk);
                }

                String flowDir = c3.getString(c3.getColumnIndex("flowDir"));
                if (flowDir != null && !flowDir.equals("null")) {
                    irrBRStadia.setFlowDir(flowDir);
                }

                irrBRStadia.setDatTp(c3.getString(c3.getColumnIndex("datTp")));
                String datElev = c3.getString(c3.getColumnIndex("datElev"));
                if (datElev != null && !datElev.equals("null")) {
                    irrBRStadia.setDatElev(new BigDecimal(datElev));
                }

                String modBasVal = c3.getString(c3.getColumnIndex("modBasVal"));
                if (modBasVal != null && !modBasVal.equals("null")) {
                    irrBRStadia.setModBasVal(new BigDecimal(modBasVal));
                }

                String modPara = c3.getString(c3.getColumnIndex("modPara"));
                if (modPara != null && !modPara.equals("null")) {
                    irrBRStadia.setModPara(new BigDecimal(modPara));
                }

                // 高程
                String elevation = c3.getString(c3.getColumnIndex("elevation"));
                if (elevation != null && !elevation.equals("null")) {
                    irrBRStadia.setElevation(new BigDecimal(elevation));
                }

                String estStYm = c3.getString(c3.getColumnIndex("estStYm"));
                if (estStYm != null && !estStYm.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(estStYm)));
                    irrBRStadia.setEstStYm(calendar);
                }

                String runCond = c3.getString(c3.getColumnIndex("runCond"));
                if (runCond != null && runCond.equals("null") && runCond.length() > 0) {
                    irrBRStadia.setRunCond(runCond);
                }

                irrBRStadia.setEngManCd(c3.getString(c3.getColumnIndex("engManCd")));

                String timeSign = c3.getString(c3.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    irrBRStadia.setTimeSign(calendar);
                }

                irrBRStadia.setRemark(c3.getString(c3.getColumnIndex("remark")));
                stadia = irrBRStadia;
                break;
            }

            c3.close();


            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        return stadia;
    }


    // 根据闸门找到渠道
    public static IrrBTCanalDetail getCanalDetail(String canalCode) {
        IrrBTCanalDetail detail = new IrrBTCanalDetail();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {
            // 获取渠道信息
            Cursor c1 = db.rawQuery("select * from t_IrrBTCanalDetail where canalCode = ?", new String[]{canalCode});
            while (c1.moveToNext()) {
                detail.setCanalCode(c1.getString(c1.getColumnIndex("canalCode")));
                detail.setCanalName(c1.getString(c1.getColumnIndex("canalName")));

                String canalG = c1.getString(c1.getColumnIndex("canalG"));
                if (canalG != null && !canalG.equals("null")) {
                    detail.setCanalG(canalG.charAt(0));
                }
//
                String canalDesQ = c1.getString(c1.getColumnIndex("canalDesQ"));
                if (canalDesQ != null && !canalDesQ.equals("null")) {
                    detail.setCanalDesQ(new BigDecimal(canalDesQ));
                }

                String leakR = c1.getString(c1.getColumnIndex("leakR"));
                if (leakR != null && !leakR.equals("null")) {
                    detail.setLeakR(new BigDecimal(leakR));
                }

                String canalLen = c1.getString(c1.getColumnIndex("canalLen"));
                if (canalLen != null && !canalLen.equals("null")) {
                    detail.setCanalLen(new BigDecimal(canalLen));
                }

                String lineLen = c1.getString(c1.getColumnIndex("lineLen"));
                if (lineLen != null && !lineLen.equals("null")) {
                    detail.setLineLen(new BigDecimal(lineLen));
                }

                detail.setEngManCd(c1.getString(c1.getColumnIndex("engManCd")));
                detail.setUperCanalCd(c1.getString(c1.getColumnIndex("uperCanalCd")));

                String timeSign = c1.getString(c1.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    detail.setTimeSign(calendar);
                }

                detail.setRemark(c1.getString(c1.getColumnIndex("remark")));

                detail.setStadiaCode(c1.getString(c1.getColumnIndex("stadiaCode")));
            }
            c1.close();
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }


        return detail;
    }


    // 获取所有的监测点
    public static List<IrrBRStadia> getAllPoint() {
        // 测站信息

        ArrayList<IrrBRStadia> stadias = new ArrayList<IrrBRStadia>();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {
            Cursor c3 = db.rawQuery("select * from t_IrrBRStadia", null);
            while (c3.moveToNext()) {
                IrrBRStadia irrBRStadia = new IrrBRStadia();
                irrBRStadia.setStadiaCode(c3.getString(c3.getColumnIndex("stadiaCode")));
                irrBRStadia.setStadiaName(c3.getString(c3.getColumnIndex("stadiaName")));
                irrBRStadia.setStadiaType(c3.getString(c3.getColumnIndex("stadiaType")));
                irrBRStadia.setLocCode(c3.getString(c3.getColumnIndex("locCode")));
                String stbk = c3.getString(c3.getColumnIndex("stbk"));
                if (stbk != null && !stbk.equals("null")) {
                    irrBRStadia.setStbk(stbk);
                }

                String flowDir = c3.getString(c3.getColumnIndex("flowDir"));
                if (flowDir != null && !flowDir.equals("null")) {
                    irrBRStadia.setFlowDir(flowDir);
                }

                irrBRStadia.setDatTp(c3.getString(c3.getColumnIndex("datTp")));
                String datElev = c3.getString(c3.getColumnIndex("datElev"));
                if (datElev != null && !datElev.equals("null")) {
                    irrBRStadia.setDatElev(new BigDecimal(datElev));
                }

                // 高程
                String elevation = c3.getString(c3.getColumnIndex("elevation"));
                if (elevation != null && !elevation.equals("null")) {
                    irrBRStadia.setElevation(new BigDecimal(elevation));
                }

                String modBasVal = c3.getString(c3.getColumnIndex("modBasVal"));
                if (modBasVal != null && !modBasVal.equals("null")) {
                    irrBRStadia.setModBasVal(new BigDecimal(modBasVal));
                }

                String modPara = c3.getString(c3.getColumnIndex("modPara"));
                if (modPara != null && !modPara.equals("null")) {
                    irrBRStadia.setModPara(new BigDecimal(modPara));
                }

                String estStYm = c3.getString(c3.getColumnIndex("estStYm"));
                if (estStYm != null && !estStYm.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(estStYm)));
                    irrBRStadia.setEstStYm(calendar);
                }

                String runCond = c3.getString(c3.getColumnIndex("runCond"));
                if (runCond != null && runCond.equals("null") && runCond.length() > 0) {
                    irrBRStadia.setRunCond(runCond);
                }

                irrBRStadia.setEngManCd(c3.getString(c3.getColumnIndex("engManCd")));

                String timeSign = c3.getString(c3.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    irrBRStadia.setTimeSign(calendar);
                }

                irrBRStadia.setRemark(c3.getString(c3.getColumnIndex("remark")));
                stadias.add(irrBRStadia);
            }

            c3.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        return stadias;
    }


    // 通过测站编码获取测站
    public static IrrBRStadia getIrrBRStadiaByCode(String code)
    {
        IrrBRStadia stadia = new IrrBRStadia();
        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {
            Cursor c3 = db.rawQuery("select * from t_IrrBRStadia where stadiaCode = ?", new String[]{code});
            while (c3.moveToNext()) {
                IrrBRStadia irrBRStadia = new IrrBRStadia();
                irrBRStadia.setStadiaCode(c3.getString(c3.getColumnIndex("stadiaCode")));
                irrBRStadia.setStadiaName(c3.getString(c3.getColumnIndex("stadiaName")));
                irrBRStadia.setStadiaType(c3.getString(c3.getColumnIndex("stadiaType")));
                irrBRStadia.setLocCode(c3.getString(c3.getColumnIndex("locCode")));
                String stbk = c3.getString(c3.getColumnIndex("stbk"));
                if (stbk != null && !stbk.equals("null")) {
                    irrBRStadia.setStbk(stbk);
                }

                String flowDir = c3.getString(c3.getColumnIndex("flowDir"));
                if (flowDir != null && !flowDir.equals("null")) {
                    irrBRStadia.setFlowDir(flowDir);
                }

                irrBRStadia.setDatTp(c3.getString(c3.getColumnIndex("datTp")));
                String datElev = c3.getString(c3.getColumnIndex("datElev"));
                if (datElev != null && !datElev.equals("null")) {
                    irrBRStadia.setDatElev(new BigDecimal(datElev));
                }

                String modBasVal = c3.getString(c3.getColumnIndex("modBasVal"));
                if (modBasVal != null && !modBasVal.equals("null")) {
                    irrBRStadia.setModBasVal(new BigDecimal(modBasVal));
                }

                String modPara = c3.getString(c3.getColumnIndex("modPara"));
                if (modPara != null && !modPara.equals("null")) {
                    irrBRStadia.setModPara(new BigDecimal(modPara));
                }

                String estStYm = c3.getString(c3.getColumnIndex("estStYm"));
                if (estStYm != null && !estStYm.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(estStYm)));
                    irrBRStadia.setEstStYm(calendar);
                }

                String runCond = c3.getString(c3.getColumnIndex("runCond"));
                if (runCond != null && runCond.equals("null") && runCond.length() > 0) {
                    irrBRStadia.setRunCond(runCond);
                }

                irrBRStadia.setEngManCd(c3.getString(c3.getColumnIndex("engManCd")));

                String timeSign = c3.getString(c3.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    irrBRStadia.setTimeSign(calendar);
                }

                // 高程
                String elevation = c3.getString(c3.getColumnIndex("elevation"));
                if (elevation != null && !elevation.equals("null")) {
                    irrBRStadia.setElevation(new BigDecimal(elevation));
                }

                irrBRStadia.setRemark(c3.getString(c3.getColumnIndex("remark")));
                stadia = irrBRStadia;
            }

            c3.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        return stadia;
    }

    // 通过类型获取站点信息
    public static List<IrrBRStadia> getPointByTypte(int type) {
        ArrayList<IrrBRStadia> stadias = new ArrayList<IrrBRStadia>();

        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {
            Cursor c3 = db.rawQuery("select * from t_IrrBRStadia where stadiaType = ?", new String[]{type+""});
            while (c3.moveToNext()) {
                IrrBRStadia irrBRStadia = new IrrBRStadia();
                irrBRStadia.setStadiaCode(c3.getString(c3.getColumnIndex("stadiaCode")));
                irrBRStadia.setStadiaName(c3.getString(c3.getColumnIndex("stadiaName")));
                irrBRStadia.setStadiaType(c3.getString(c3.getColumnIndex("stadiaType")));
                irrBRStadia.setLocCode(c3.getString(c3.getColumnIndex("locCode")));
                String stbk = c3.getString(c3.getColumnIndex("stbk"));
                if (stbk != null && !stbk.equals("null")) {
                    irrBRStadia.setStbk(stbk);
                }

                String flowDir = c3.getString(c3.getColumnIndex("flowDir"));
                if (flowDir != null && !flowDir.equals("null")) {
                    irrBRStadia.setFlowDir(flowDir);
                }

                irrBRStadia.setDatTp(c3.getString(c3.getColumnIndex("datTp")));
                String datElev = c3.getString(c3.getColumnIndex("datElev"));
                if (datElev != null && !datElev.equals("null")) {
                    irrBRStadia.setDatElev(new BigDecimal(datElev));
                }

                String modBasVal = c3.getString(c3.getColumnIndex("modBasVal"));
                if (modBasVal != null && !modBasVal.equals("null")) {
                    irrBRStadia.setModBasVal(new BigDecimal(modBasVal));
                }

                String modPara = c3.getString(c3.getColumnIndex("modPara"));
                if (modPara != null && !modPara.equals("null")) {
                    irrBRStadia.setModPara(new BigDecimal(modPara));
                }

                String estStYm = c3.getString(c3.getColumnIndex("estStYm"));
                if (estStYm != null && !estStYm.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(estStYm)));
                    irrBRStadia.setEstStYm(calendar);
                }

                String runCond = c3.getString(c3.getColumnIndex("runCond"));
                if (runCond != null && runCond.equals("null") && runCond.length() > 0) {
                    irrBRStadia.setRunCond(runCond);
                }

                irrBRStadia.setEngManCd(c3.getString(c3.getColumnIndex("engManCd")));

                String timeSign = c3.getString(c3.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    irrBRStadia.setTimeSign(calendar);
                }

                irrBRStadia.setRemark(c3.getString(c3.getColumnIndex("remark")));
                stadias.add(irrBRStadia);
            }

            c3.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        return stadias;
    }

    // 获取水情站信息
    public static List<IrrBRStadia> getWaterPoint() {
        ArrayList<IrrBRStadia> stadias = new ArrayList<IrrBRStadia>();



        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();

        try {
            Cursor c3 = db.rawQuery("SELECT s.*,g.orderNum FROM t_IrrBAWaterGate g, t_IrrBRStadia s WHERE g.watergateCode = s.locCode AND s.stadiaType=? ORDER BY g.orderNum ASC", new String[]{Type.WATER+""});
            while (c3.moveToNext()) {
                int i = c3.getInt(c3.getColumnIndex("orderNum"));
                IrrBRStadia irrBRStadia = new IrrBRStadia();
                irrBRStadia.setStadiaCode(c3.getString(c3.getColumnIndex("stadiaCode")));
                irrBRStadia.setStadiaName(c3.getString(c3.getColumnIndex("stadiaName")));
                irrBRStadia.setStadiaType(c3.getString(c3.getColumnIndex("stadiaType")));
                irrBRStadia.setLocCode(c3.getString(c3.getColumnIndex("locCode")));
                String stbk = c3.getString(c3.getColumnIndex("stbk"));
                if (stbk != null && !stbk.equals("null")) {
                    irrBRStadia.setStbk(stbk);
                }

                String flowDir = c3.getString(c3.getColumnIndex("flowDir"));
                if (flowDir != null && !flowDir.equals("null")) {
                    irrBRStadia.setFlowDir(flowDir);
                }

                irrBRStadia.setDatTp(c3.getString(c3.getColumnIndex("datTp")));
                String datElev = c3.getString(c3.getColumnIndex("datElev"));
                if (datElev != null && !datElev.equals("null")) {
                    irrBRStadia.setDatElev(new BigDecimal(datElev));
                }

                String modBasVal = c3.getString(c3.getColumnIndex("modBasVal"));
                if (modBasVal != null && !modBasVal.equals("null")) {
                    irrBRStadia.setModBasVal(new BigDecimal(modBasVal));
                }

                String modPara = c3.getString(c3.getColumnIndex("modPara"));
                if (modPara != null && !modPara.equals("null")) {
                    irrBRStadia.setModPara(new BigDecimal(modPara));
                }

                String estStYm = c3.getString(c3.getColumnIndex("estStYm"));
                if (estStYm != null && !estStYm.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(estStYm)));
                    irrBRStadia.setEstStYm(calendar);
                }

                String runCond = c3.getString(c3.getColumnIndex("runCond"));
                if (runCond != null && runCond.equals("null") && runCond.length() > 0) {
                    irrBRStadia.setRunCond(runCond);
                }

                // 高程
                String elevation = c3.getString(c3.getColumnIndex("elevation"));
                if (elevation != null && !elevation.equals("null")) {
                    irrBRStadia.setElevation(new BigDecimal(elevation));
                }

                irrBRStadia.setEngManCd(c3.getString(c3.getColumnIndex("engManCd")));

                String timeSign = c3.getString(c3.getColumnIndex("timeSign"));
                if (timeSign != null && !timeSign.equals("null")) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(Long.parseLong(timeSign)));
                    irrBRStadia.setTimeSign(calendar);
                }

                irrBRStadia.setRemark(c3.getString(c3.getColumnIndex("remark")));
                stadias.add(irrBRStadia);
            }

            c3.close();

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }

        // 给stadias排序
       /* HashMap<String,IrrBRStadia> stadiaHashMap = new HashMap<>();
        for (IrrBRStadia stadia:stadias)
        {
            if (stadia.getStadiaName().endsWith("水情监测点"))
            {
                String name = stadia.getStadiaName().replace("水情监测点","");
                stadiaHashMap.put(name,stadia);
            }
            else
            {
                stadiaHashMap.put(stadia.getStadiaName(),stadia);
            }
        }

        String[] waterarrays = MyApplication.share().getResources().getStringArray(R.array.waterarrays);
        ArrayList<IrrBRStadia> orderedStadia = new ArrayList<IrrBRStadia>();
        for (int i=0;i<waterarrays.length;i++)
        {
            orderedStadia.add(stadiaHashMap.get(waterarrays[i]));
        }*/

        return stadias;

    }

    // 通过测站获取闸门信息
    public static IrrBAWaterGate getwaterGateByStadia(String locCode)
    {
        if (locCode == null)return null;
        if (locCode.startsWith("G")||locCode.startsWith("E"))
        {

            IrrBAWaterGate irrBAWaterGate = null;
            SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
            db.beginTransaction();

            try {

                // 获取闸门信息
                Cursor c2 = db.rawQuery("select * from t_IrrBAWaterGate where watergateCode=?", new String[]{locCode});
                while (c2.moveToNext()) {
                    IrrBAWaterGate gate = new IrrBAWaterGate();
                    gate.setWatergateCode(c2.getString(c2.getColumnIndex("watergateCode")));
                    gate.setCanalCode(c2.getString(c2.getColumnIndex("canalCode")));
                    gate.setWatergateName(c2.getString(c2.getColumnIndex("watergateName")));
                    gate.setWatergateType(c2.getString(c2.getColumnIndex("watergateType")));
                    gate.setWatergateMET(c2.getString(c2.getColumnIndex("watergateMET")));
                    gate.setWatergateLC(c2.getString(c2.getColumnIndex("watergateLC")));


                    String watergateNUM = c2.getString(c2.getColumnIndex("watergateNUM"));
                    if (watergateNUM != null && !watergateNUM.equals("null")) {
                        gate.setWatergateNUM(new BigDecimal(watergateNUM));
                    }

                    String gateGaocheng = c2.getString(c2.getColumnIndex("gaocheng"));
                    if (gateGaocheng != null && !gateGaocheng.equals("null"))
                    {
                        gate.setFloorElevation(new BigDecimal(gateGaocheng));
                    }

                    String watergateHeight = c2.getString(c2.getColumnIndex("watergateHeight"));
                    if (watergateHeight != null && !watergateHeight.equals("null")) {
                        gate.setWatergateHeight(new BigDecimal(watergateHeight));
                    }

                    String watergateWtdth = c2.getString(c2.getColumnIndex("watergateWtdth"));
                    if (watergateWtdth != null && !watergateWtdth.equals("null")) {
                        gate.setWatergateWtdth(new BigDecimal(watergateWtdth));
                    }

                    String watergateQ = c2.getString(c2.getColumnIndex("watergateQ"));
                    if (watergateQ != null && !watergateQ.equals("null")) {
                        gate.setWatergateQ(new BigDecimal(watergateQ));
                    }

                    String desingWaterLevel = c2.getString(c2.getColumnIndex("designWaterLevel"));
                    if (desingWaterLevel != null && !desingWaterLevel.equals("null"))
                    {
                        gate.setDesignWaterLevel(desingWaterLevel);
                    }

                    String watergateMTP = c2.getString(c2.getColumnIndex("watergateMTP"));
                    if (watergateMTP != null && !watergateMTP.equals("null")) {
                        gate.setWatergateMTP(watergateMTP);
                    }

                    String watergateStatus = c2.getString(c2.getColumnIndex("watergateStatus"));
                    if (watergateStatus != null && !watergateStatus.equals("null") && watergateStatus.length() > 0) {
                        gate.setWatergateStatus(watergateStatus.charAt(0));
                    }

                    String designWaterLevel = c2.getString(c2.getColumnIndex("designWaterLevel"));
                    if (designWaterLevel != null && !designWaterLevel.equals("null"))
                    {
                        gate.setWatergateQ(new BigDecimal(designWaterLevel));
                    }

                    gate.setEngManCd(c2.getString(c2.getColumnIndex("engManCd")));
                    gate.setTelephone(c2.getString(c2.getColumnIndex("telephone")));

                    String timeSign = c2.getString(c2.getColumnIndex("timeSign"));
                    if (timeSign != null && !timeSign.equals("null")) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(new Date(Long.parseLong(timeSign)));
                        gate.setTimeSign(calendar);
                        gate.setRemark(c2.getString(c2.getColumnIndex("remark")));
                    }

                    gate.setStadiaCode(c2.getString(c2.getColumnIndex("stadiaCode")));
                    irrBAWaterGate = gate;

                }

                c2.close();

                db.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                db.endTransaction();
                DatabaseManager.getInstance().closeDatabase();
            }
            return irrBAWaterGate;
        }

        return null;
    }


    // 获取雨情站信息
    public static List<IrrBRStadia> getRainPoint() {
        return getPointByTypte(Type.RAIN);
    }

    // 根据指定的闸门id选择此闸门的管理人员的电话号码
    public static String getPhoneNumber(String gateCode) {
        String number = null;
        SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
        db.beginTransaction();
        try {
            Cursor cursor =  db.rawQuery("select telephone from t_IrrBAWaterGate where watergateCode = ?", new String[]{gateCode});
            cursor.moveToFirst();
            number = cursor.getString(cursor.getColumnIndex("telephone"));

            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
            DatabaseManager.getInstance().closeDatabase();
        }
        return number;
    }
}
